/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	ADeviceWeibaoServiceImpl.java
 * Created：		2016年9月14日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.device.weibao;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.device.weibao.converter.ADeviceWeibaoConfigConverter;
import com.thor.palmmall.rest.device.weibao.converter.ADeviceWeibaoConverter;
import com.thor.palmmall.rest.device.weibao.converter.SDeviceWeibao4CreateConverter;
import com.thor.palmmall.rest.device.weibao.converter.SDeviceWeibaoConverter;
import com.thor.palmmall.rest.repair.ARepairBillQueryParam;
import com.thor.palmmall.rest.repair.converter.SMaintainBillQueryParamConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.device.weibao.SDeviceWeibao;
import com.thor.palmmall.spi.device.weibao.SDeviceWeibaoConfig;
import com.thor.palmmall.spi.device.weibao.SDeviceWeibaoService;

/**
 * @author wolv
 *
 */
public class ADeviceWeibaoServiceImpl implements ADeviceWeibaoService {

  @Override
  public ADeviceWeibaoConfig getConfig(String storeUuid) throws ThorServiceException {
    Assert.assertArgumentNotNull(storeUuid, "storeUuid");

    SResponse<SDeviceWeibaoConfig> resp = sDeviceWeibaoService.getConfig(storeUuid);
    if (resp.isSuccess()) {
      return ADeviceWeibaoConfigConverter.getInstance().convert(resp.getBody());
    } else
      throw new ThorServiceException("无法找到指定的维保单配置：" + resp.getMessage());
  }

  @Override
  public String create(ADeviceWeibao4Create bill, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sDeviceWeibaoService.create(
        SDeviceWeibao4CreateConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法创建维保单：" + resp.getMessage());

  }

  @Override
  public ADeviceWeibao get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    Assert.assertArgumentNotNull(id, "id");
    Assert.assertArgumentNotNull(param, "param");

    SResponse<SDeviceWeibao> resp = sDeviceWeibaoService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return ADeviceWeibaoConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的维保单：" + resp.getMessage());
  }

  @Override
  public String save(ADeviceWeibao bill, BeanOperateInfo operateInfo) throws ThorServiceException {
    Assert.assertArgumentNotNull(bill, "bill");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sDeviceWeibaoService.save(
        SDeviceWeibaoConverter.getInstance().convert(bill),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存维保单：" + resp.getMessage());
  }

  @Override
  public QueryResult<ADeviceWeibao> query(ARepairBillQueryParam queryParam)
      throws ThorServiceException {
    SResponse<SQueryResult<SDeviceWeibao>> resp = sDeviceWeibaoService
        .query(SMaintainBillQueryParamConverter.getInstance().convert(queryParam));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), ADeviceWeibaoConverter.getInstance());
    else
      throw new ThorServiceException("无法查询维保单：" + resp.getMessage());
  }

  @Override
  public String[] queryOutgoingState() throws ThorServiceException {
    SResponse<String[]> resp = sDeviceWeibaoService.queryOutgoingState();
    if (resp.isSuccess()) {
      return resp.getBody();
    } else {
      throw new ThorServiceException("查询流程状态失败：" + resp.getMessage());
    }
  }

  @Autowired
  private SDeviceWeibaoService sDeviceWeibaoService;

}
